package com.rabbitmq.orderservice.controller;

import com.rabbitmq.orderservice.constant.Constants;
import com.rabbitmq.orderservice.model.OrderMessageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 订单系统发送消息
 */
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/sendMessage")
    public String sendOrderMessage() {

        OrderMessageInfo orderMessageInfo = new OrderMessageInfo();
        orderMessageInfo.setOrderId(UUID.randomUUID().toString());
        orderMessageInfo.setMessageInfo("商品aaa");

        //将对象以 json 格式发送
        rabbitTemplate.convertAndSend("", Constants.ORDER_QUEUE, orderMessageInfo);

        return "订单消息发送成功";
    }
}
